package com.financing.wap.service.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.financing.wap.dao.IForegroundProductDao;
import com.financing.wap.dto.ForegroundProductDTO;
import com.financing.wap.dto.OutProductDTO;
import com.financing.wap.service.IForegroundProductService;

@Repository("foregroundProductService")
public class ForegroundProductServiceImpl implements IForegroundProductService {
	@Autowired
	private IForegroundProductDao foregroundProductDao;
	
	@Override
	public int addForegroundProduct(ForegroundProductDTO foregroundProductDto) {
		return foregroundProductDao.addForegroundProduct(foregroundProductDto);
	}

	@Override
	public List<ForegroundProductDTO> queryForegroundProduct(
			ForegroundProductDTO foregroundProductDto) {
		return foregroundProductDao.queryForegroundProduct(foregroundProductDto);
	}

	@Override
	@Transactional(rollbackFor={Exception.class,RuntimeException.class})
	public int updateOutProduct(OutProductDTO outProductDto,ForegroundProductDTO foregroundProDto) throws SQLException {
		String oldOtherInstructions=foregroundProDto.getOtherInstructions();
		foregroundProDto.setOtherInstructions(outProductDto.getOtherInstructions());
		foregroundProductDao.updateForegroundProduct(foregroundProDto);
		if( foregroundProDto.getpIssuingScale()!=null && outProductDto.getpIssuingScale() !=null && foregroundProDto.getpIssuingScale().compareTo(outProductDto.getpIssuingScale())!=0){
			//历史募集规模 修改年化率
			Map<String,Object> map = new HashMap<String,Object> ();
			map.put("newAmount", outProductDto.getpIssuingScale());
			map.put("oldAmount", foregroundProDto.getpIssuingScale());
			map.put("pId", foregroundProDto.getpId());
			map.put("updateBy", outProductDto.getUpdateBy());
			foregroundProductDao.updateProjectAnnualRevenueByendAmount(map);
		}
		
		foregroundProductDao.updateOutProduct(outProductDto);
		
		//修改募集进度为历史
		outProductDto.setpRecruitmentSummary(foregroundProDto.getpRecruitmentSummary());
		outProductDto.setOtherInstructions(oldOtherInstructions);
		outProductDto.setpPeriods(foregroundProDto.getpPeriods());
		outProductDto.setpIssuingScale(foregroundProDto.getpIssuingScale());
		outProductDto.setpAllSubscriptionAmount(foregroundProDto.getpAllSubscriptionAmount());
		//新增日志
		foregroundProductDao.addForegroundProductLog(outProductDto);
		
		return 1;
	}

	@Override
	public int countForegroundProduct(ForegroundProductDTO foregroundProductDto) {
		return foregroundProductDao.countForegroundProduct(foregroundProductDto);
	}

	

	
}
